/**
 * @method 封装jsonp
 * @author Maxma
 */

import originJson from 'jsonp'

export default function jsonp(
  url: string,
  data: object,
  option: object
): object {
  url += url.indexOf('?') < 0 ? '?' : '&'
  return new Promise((resolve, reject) => {
    originJson(url, option, (err, data) => {
      if (!err) {
        resolve(data)
      } else {
        reject(err)
      }
    })
  })
}

function params(data: any): string {
  if (Object.keys(data).length > 0) {
    let url = ''
    for (let i in data) {
      let value = data[i] !== undefined ? data[i] : ''
      url += `&${i}=${encodeURIComponent(value)}`
    }
    return url ? url.substring(1) : url
  } else {
    return ''
  }
}
